package com.smasher.certificate.register

import org.gradle.api.Project

class QueryApkPath {

    static def getAllApkPaths(Project target) {
        def apkPaths = [:]

        try {
            // 方法1: 通过Variant API获取
            def applicationVariants = target.android.applicationVariants

            applicationVariants.configureEach { variant ->
                def variantName = variant.name
                try {

                    def packageTask = variant.getPackageApplicationProvider().get()
                    def apkOutputDir = packageTask.outputDirectory.get()

                    def apkOutputFileDir = apkOutputDir.asFile

                    def file = new File(apkOutputFileDir.absolutePath)
                    def files = file.list()
                    files.each { targetPath ->
                        if (targetPath.endsWith(".apk")) {
                            apkPaths[variantName] = file.absolutePath + File.separator + targetPath
                        }
                    }

                } catch (Exception e) {
                    println "获取 ${variantName} 变体APK文件夹路径时出错: ${e.message}"
                }
            }
        } catch (Exception e) {
            println "通过Variant API获取APK文件夹路径失败: ${e.message}"
        }

        return apkPaths
    }
}